<?

//==================================================================
//
//  Author: Matthew Saragusa
//  email: polyhedron@gmail.com
//  Name: A Simple MySQL Class
//  Description: This class is written to make it easy to use mysql
//  with array variables. This is exceptionally useful when dealing
//  with HTML Post variables.
//
//===================================================================
//
// USER VARIABLES (Change these only!)

/*
 * server:
  host: mysql.arena-park.ch
  database: arenaparkch
  user:arenaparkch
  pass: adminarenaparkch
 */

define('SIMPLE_DB_SERVER', 'localhost');
define('SIMPLE_DB_NAME', 'arena-park');
define('SIMPLE_DB_USERNAME', 'root');
define('SIMPLE_DB_PASSWORD', 'root');

//define('SIMPLE_DB_SERVER', 'mysql.arena-park.ch');
//define('SIMPLE_DB_NAME', 'arenaparkch');
//define('SIMPLE_DB_USERNAME', 'arenaparkch');
//define('SIMPLE_DB_PASSWORD', 'adminarenaparkch');
// END USER VARIABLES
//
//===================================================================


class ASimpleMySQLDB {

	var $db, $conn;

	public function __construct($server, $database, $username, $password) {

		$this->conn = mysql_connect($server, $username, $password);
		$this->db = mysql_select_db($database, $this->conn);
	}

	static function normalizeQuery($request) {	

		$result = array();
		foreach ($request as $column) {
			if (isset($column[1])) {


				if ($column[0] === "phone_movil") {
					$column[1] = str_replace(" ", "", $column[1]);
					$column[1]= str_replace(".", "", $column[1]);
					$column[1] = str_replace("-", "", $column[1]);
					$column[1] = str_replace("(", "", $column[1]);
					$column[1] = str_replace(")", "", $column[1]);
					$column[1] = str_replace("+", "", $column[1]);
					$result[$column[0]] = $column[1];
				}
				else
					$result[$column[0]] = $column[1];
			}		
				
		}
		return $result;
	}

	public function insert_array($table, $insert_values, $directFromAjax = true) {

		if ($directFromAjax)
			$insert_values = $this->normalizeQuery($insert_values);

		foreach ($insert_values as $key => $value) {
			$keys[] = $key;
			$insertvalues[] = '\'' . mysql_real_escape_string($value) . '\'';
		}

		$keys = implode(',', $keys);
		$insertvalues = implode(',', $insertvalues);

		$sql = "INSERT INTO $table ($keys) VALUES ($insertvalues)";

		return $this->sqlordie($sql);
	}

//	public function update_array($table, $keyColumnName, $id, $update_values) {
//
//
//		foreach ($update_values as $key => $value) {
//
//			$sets[] = $key . '=\'' . $value . '\'';
//		}
//		$sets = implode(',', $sets);
//
//		$sql = "UPDATE $table SET $sets WHERE $keyColumnName = '$id'";
//
//		$this->sqlordie($sql);
//	}
	public function get_last_id($table) {

		$id = null;

		switch ($table) {
			case "user":
				$id = "userid";
				break;
			case "reservation":
				$id = "reservation_number";
				break;
			case "car":
				$id = "idcar";
				break;
			case "conductor":
				$id = "conductorid";
				break;

			default:
				break;
		}

		$sql = "SELECT Max($id) FROM $table";

		$result = $this->sqlordie($sql);

		$result = mysql_fetch_assoc($result);

		return $result["Max($id)"];
	}

	public function get_record_by_ID($table, $keyColumnName, $id, $fields = "*") {

		$sql = "SELECT $fields FROM $table WHERE $keyColumnName = '$id'";

		$result = $this->sqlordie($sql);

		return mysql_fetch_assoc($result);
	}

	public function get_record_by_fields($table, $fields = array()) {

		$resultTable = [];
		$fieldsString = "*";
		if (count($fields))
			foreach ($fields as $value) {

				$fieldsString .= $value . ' ,';
			}
		$fieldsString = substr($fieldsString, 0, -1);

		$sql = "SELECT $fieldsString FROM $table";

		$result = $this->sqlordie($sql);

		while ($resultTable[] = mysql_fetch_assoc($result)) {

			true;
		}

		return $resultTable;
	}

	public function exec_query($query) {

		$resultTable = [];

		if (!( $result = $this->sqlordie($query)))
			return false;
		$resultTable = array();
		$row;
		while ($row = mysql_fetch_assoc($result)) {
			$resultTable[] = $row;
			true;
		}
		
		return (count($resultTable) && $resultTable[0] ) ? $resultTable : false;
	}
	
	public function exec_query_update($query) {

		return $this->sqlordie($query);
	}

	public function get_records_by_group($table, $groupKeyName, $groupID, $orderKeyName = '', $order = 'ASC', $fields = '*') {

		$orderSql = '';
		if ($orderKeyName != '')
			$orderSql = " ORDER BY $orderKeyName $order";
		$sql = "SELECT * FROM $table WHERE $groupKeyName = '$groupID'" . $orderSql;

		$result = $this->sqlordie($sql);

		while ($row = mysql_fetch_assoc($result)) {

			$records[] = $row;
		}

		return (isset($records)) ? $records : false;
	}

	private function sqlordie($sql) {

		$return_result = mysql_query($sql, $this->conn);
		if ($return_result) {
			return $return_result;
		} else {
			return false;
		}
	}

	private function sql_error($sql) {
		echo mysql_error($this->conn) . '<br>';
		die('error: ' . $sql);
	}

	private function closeConexion() {

		return mysql_close($this->conn);
	}

}

?>